<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use Imi\Server\Route\Annotation\Middleware;

/**
 * 演示
 * @Controller("/Index/")
 * @View(baseDir="Index")
 */
class Index extends HttpController
{
    /**
     * PHP 原生模版引擎演示
     * 访问：http://127.0.0.1:8080/
     * 
     * @Action
     * @Route(url="/")
     * @View(template="index")
     * 
     * 单个中间件，浏览器 F12 看Response Header：
     * @Middleware(\ImiDemo\HttpDemo\Middlewares\PoweredBy::class)
     * @return void
     */
    public function index()
    {
        return [
            'title'        =>    'hello imi',
            'content'    =>    'imi is very six',
        ];
    }

    /**
     * 无@View注解，不用写代码，也可以渲染模版
     * 访问：http://127.0.0.1:8080/Index/test
     * 
     * @Action
     * 
     * 多个中间件，浏览器 F12 看Response Header：
     * @Middleware({
     * \ImiDemo\HttpDemo\Middlewares\PoweredBy::class,
     * \ImiDemo\HttpDemo\Middlewares\Test::class
     * })
     * 
     * @return void
     */
    public function test()
    {

    }

    /**
     * api 演示
     * 访问：http://127.0.0.1:8080/Index/api
     * 
     * @Action
     * @Route(url="api")
     * @View(renderType="json")
     * @return void
     */
    public function api()
    {
        return [
            'id'    =>    1,
            'name'    =>    'imi',
        ];
    }

    /**
     * GET/POST参数 演示
     * 访问：http://127.0.0.1:8080/Index/params?id=123&name=imi
     * 
     * @Action
     * @Route(url="params")
     * @View(renderType="json")
     * @return void
     */
    public function params($id)
    {
        return [
            'id'        =>    $id,
            'name'        =>    $this->request->get('name', 'default'),
            'getParams'    =>    $this->request->get(),
            'postParams'=>    $this->request->post(),
        ];
    }
}